package com.bete.updata;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.View;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import me.jessyan.autosize.utils.LogUtils;

/**
 * * <p/>
 * Created by C.L.Wang on 15/7/27.
 */
public class ScreenShotUtil {

    /**
     * 保存一个view的截图
     *
     * @param view
     * @param filepath
     * @return
     */
    public static boolean screenShots(View view, String filepath) {
        boolean flag = false;
//        Bitmap bitmap = getCacheBitmap(view);
        Bitmap bitmap = getBitmapFromView(view);
        if (bitmap != null) {
            String path = filepath;
            FileCommonUtil.deleteFile(path);
            FileCommonUtil.createOrExistsFile(path);
            saveBitmapAsPng(bitmap, new File(path));
        }else{
            LogUtils.d("bitmap != null");
        }
        return flag;
    }


    public static void saveBitmapAsPng(Bitmap bmp, File f) {
        try {
            FileOutputStream out = new FileOutputStream(f);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
            LogUtils.d("已保存至" + f.getAbsolutePath() + "^.^");
        } catch (FileNotFoundException e) {
            LogUtils.d(e.toString());
        } catch (IOException e) {
            LogUtils.d(e.toString());
        }
    }

    /**
     * 获取一个 View 的缓存视图
     *
     * @param view
     * @return
     */
    public static Bitmap getCacheBitmap(View view) {
        final boolean drawingCacheEnabled = true;
        view.setDrawingCacheEnabled(drawingCacheEnabled);
        view.buildDrawingCache(drawingCacheEnabled);
        final Bitmap drawingCache = view.getDrawingCache();
        Bitmap bitmap;
        if (drawingCache != null) {
            bitmap = Bitmap.createBitmap(drawingCache);
            view.setDrawingCacheEnabled(false);
        } else {
            bitmap = null;
        }
        return bitmap;
    }

    private static Bitmap getBitmapFromView(View view) {
        Bitmap bitmap = null;
        try {
            int width = view.getWidth();
            int height = view.getHeight();
            if(width != 0 && height != 0){
                bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bitmap);
                view.layout(0, 0, width, height);
                view.draw(canvas);
            }
        } catch (Exception e) {
            bitmap = null;
            LogUtils.d(e.toString());
        }
        return bitmap;
//        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
//                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
//        view.setDrawingCacheEnabled(true);
//        view.buildDrawingCache();
//        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
//        view.setDrawingCacheEnabled(false);
//        return bitmap;
    }

}